package monster.flight.comm.util.mobile.factory;

import java.rmi.RemoteException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

//import jsoft.com.util.DBManager;
//import jsoft.com.util.mobliemsg.MGCfilter;

import org.apache.log4j.Logger;
import sun.jdbc.rowset.CachedRowSet;
public class SendThread extends Thread {
	/**
	private Logger gLog = Logger.getLogger("SendThread");
	private DBManager db; 
	public  SendThread(){
		try {
			db = new DBManager();
		} catch (Exception e) {
			e.printStackTrace();
		}
		gLog.info("client SendThread线程启动...");
	}
	public void run() {
		gLog.info("client SendThread线程执行中...");
		CachedRowSet rs;		
		while (true) {
			try {
				long time = 0;
				long begin = System.nanoTime();// 
				  rs = db.getRowSet("select top 600  DX_SN,REC_MOBILE,SEND_CONT from IC_MOBILE_LETTER where LOCK_STATE='0' and SEND_STATE = '01'   ");
				  if(rs.size()>0){				
					StringBuffer mobilesb = new StringBuffer();
					StringBuffer contsb = new StringBuffer();
					HashMap<String, String> map = new HashMap<String, String>(600);
					while (rs.next()) {
						String remoteID = rs.getString("dx_sn");						
						String mobile = rs.getString("REC_MOBILE");
						String cont = rs.getString("SEND_CONT");
						mobilesb.append(mobile).append(",");
						contsb.append(MGCfilter.can_Pass(cont)).append("`");					
						map.put(remoteID, mobile);
					}
					//得到失败字符串
					String returnStr = MobileFactory.banctSendInfo(mobilesb.toString(), contsb.toString());
					ArrayList<String> list = new ArrayList<String>(600);
					if(!returnStr.equals("1")){//有发送失败的
						String [] strs = returnStr.split("\\|\\|");
						HashMap<String, String> falseMap = new HashMap<String, String>(600);
						int len = strs.length;
						for(int i=0;i<len;i++){
							String falsesStr = strs[i];
							if(!falsesStr.equals("")){
								String [] v = falsesStr.split("#");
								falseMap.put(v[0], falsesStr);
							}							
						}						
						for(Iterator<String> it = map.keySet().iterator();it.hasNext();){
							String remoteID = it.next();
							String mobile = map.get(remoteID);
							if(falseMap.containsKey(mobile)){
								list.add("update IC_MOBILE_LETTER set SEND_FAIL_REASON='"+ falseMap.get(mobile)+ "',SEND_STATE = '03',LOCK_STATE='0' ,send_time=convert(varchar(100),getdate(),20) where  dx_sn="+remoteID);
							}else{
								list.add("update IC_MOBILE_LETTER set SEND_STATE = '02',LOCK_STATE='0' ,send_time=convert(varchar(100),getdate(),20) where  dx_sn="+remoteID);
							}						
						}
					}else{//成功
						for(Iterator<String> it = map.keySet().iterator();it.hasNext();){
							String remoteID = it.next();
							list.add("update IC_MOBILE_LETTER set SEND_STATE = '02',LOCK_STATE='0' ,send_time=convert(varchar(100),getdate(),20) where dx_sn="+remoteID);
												
						}
					}
					int len = list.size();
					if(len>0){
						String [] sqls = new String[len];
						list.toArray(sqls);
						db.batchMoreUpdate(sqls);
					}
					long end = System.nanoTime();
					time += (end - begin) / 1000000;
					//gLog.info("[成功提交：" + j + " 条] [时间为: " + time + "毫秒]");
					//gLog.info("找到要发送的数据，休息5分钟");
					Thread.sleep(300000);//找到要发送的数据，休息5分钟
					
				} else {
					//gLog.info("未找到要发送的数据，休息10分钟");
					Thread.sleep(600000);// 未到到要发送的数据，休息10分钟
					
				}

			} catch (SQLException e) {
				e.printStackTrace();
				gLog.error(e.getMessage());
			} catch (RemoteException e) {
				gLog.error("服务端连接失败!");
				e.printStackTrace();
				try {
					Thread.sleep(600000 * 3);
				} catch (InterruptedException e1) {
					e1.printStackTrace();
				}
			} catch (InterruptedException e) {
				e.printStackTrace();
				try {
					Thread.sleep(600000 * 5);
				} catch (InterruptedException e1) {
					e1.printStackTrace();
				}
			}catch(Exception e){
				e.printStackTrace();
				try {
					Thread.sleep(600000 * 5);
				} catch (InterruptedException e1) {
					e1.printStackTrace();
				}
			}
		}
	}
	**/

}
